package 链表.反转链表_206;
import 链表.两两交换链表中的节点_24.Solution.ListNode;
/*
给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。


示例 1：
输入：head = [1,2,3,4,5]
输出：[5,4,3,2,1]

示例2：
输入：head = [1,2]
输出：[2,1]

示例 3：
输入：head = []
输出：[]
* */

public class Solution {
    public static void main(String[] args) {
        ListNode[] head = new ListNode[4];
        head[3] = new ListNode(-4,null);
        head[2] = new ListNode(0,head[3]);
        head[1] = new ListNode(2,head[2]);
        head[0] = new ListNode(1,head[1]);
        System.out.println(reverseList(head[0]));
    }
    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode now = head;
        ListNode next;
        while (now != null){
            next = now.next;
            now.next = pre;
            pre = now;
            now = next;
        }
        return pre;
    }
}
